﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using CMTVision.Widgets;

namespace CMTVision.Widgets.Framework
{
    public class BaseTemplateColumn : System.Web.UI.UserControl, CMTVision.Widgets.Framework.ITemplateColumn
    {
        #region Ctor

        public BaseTemplateColumn()
            : base()
        {
            IsInEditMode = false;
        }

        #endregion
         
        #region Implement ITemplateColumn interface

        public long TemplateID
        {
            get;
            set;
        }

        public string ColumnID
        {
            get;
            set;
        }

        public virtual bool IsInEditMode
        {
            get;
            set;
        }

        public System.Web.UI.Control ContainersHolder
        {
            get
            {
                return GetColumnHolder();
            }
        }

        public void LoadWidgets()
        {
            ContainersHolder.Controls.Clear();
            List<Model.Widget> allWidgets = new List<Model.Widget>();
            using (Model.CMTWidgetsEntities entities = CMTVision.Common.EntitiesManager.LoadWidgetEntity())
            {
                allWidgets = (from w in entities.Widgets.Include("WidgetType").Include("Template")
                              where w.ColumnID == ColumnID
                                  && w.Template.ID == TemplateID
                                  && w.WidgetType != null
                              orderby w.OrderNo
                              select w).ToList();
            }

            foreach (Model.Widget widgetInfo in allWidgets)
            {
                IWidgetContainer widgetContainer = CreateNewWidgetContainer(widgetInfo.ID);
                if (widgetContainer == null)
                {
                    throw new InvalidOperationException(String.Format("Can't load the Container of Widget: {0}", widgetInfo.ID).ToString());
                }
                (widgetContainer as System.Web.UI.Control).ID = Util.GenerateID<IWidgetContainer>(widgetInfo.ID.ToString());
                widgetContainer.IsInEditMode = IsInEditMode;
                widgetContainer.LoadWidget(widgetInfo.ID);
                ContainersHolder.Controls.Add(widgetContainer as System.Web.UI.Control);
            }
        }

        public IWidgetContainer CreateNewWidgetContainer(long widgetId)
        {
            Model.Widget widgetInfo = null;
            using (Model.CMTWidgetsEntities entities = CMTVision.Common.EntitiesManager.LoadWidgetEntity())
            {
                widgetInfo = (from w in entities.Widgets.Include("WidgetContainerType") where w.ID == widgetId select w).FirstOrDefault();
            }

            var widgetContainerSrc = String.Empty;
            if (widgetInfo != null && widgetInfo.WidgetContainerType != null)
            {
                widgetContainerSrc = widgetInfo.WidgetContainerType.Src;
            }
            if (!String.IsNullOrEmpty(widgetContainerSrc))
            {
                return Page.LoadControl(widgetContainerSrc) as IWidgetContainer;
            }

            return null;
        }

        public virtual void Refresh()
        {
            if (ContainersHolder != null)
            {
                foreach (System.Web.UI.Control control in ContainersHolder.Controls)
                {
                    var widgetContainer = control as CMTVision.Widgets.Framework.IWidgetContainer;
                    if (widgetContainer != null)
                    {
                        widgetContainer.IsInEditMode = IsInEditMode;
                        widgetContainer.Refresh();
                    }
                }
            }
        }

        #endregion

        #region Other  Methods

        protected virtual System.Web.UI.Control GetColumnHolder()
        {
            throw new InvalidOperationException("Did not assign value to ColumnHoder.");
        }
        
        #endregion   
    
          
    

        public ColumnWidgetLockMode LockMode
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
}